package com.cqie.hospitalescort.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.VO.AssessInfoVO;
import com.cqie.hospitalescort.entity.AssessInfo;
import com.cqie.hospitalescort.enums.RoleEnum;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.mapper.AssessInfoMapper;
import com.cqie.hospitalescort.service.AssessInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.hospitalescort.utils.Asserts;
import com.cqie.hospitalescort.utils.WebSecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 评价信息 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@Service
public class AssessInfoServiceImpl extends ServiceImpl<AssessInfoMapper, AssessInfo> implements AssessInfoService {

    @Autowired
    private AssessInfoMapper assessInfoMapper;

    /**
     * 评论列表
     *
     * @param escortId
     * @return
     */
    @Override
    public List<AssessInfoVO> getAssessInfo(Long escortId) {
        AuthUser currentUser = WebSecurityUtil.getCurrentUser();

        return assessInfoMapper.selectList(new LambdaQueryWrapper<AssessInfo>()
                        .eq(AssessInfo::getEscortId, escortId)) // 直接使用查询结果，无需判空
                .stream()
                .map(assessInfo -> {
                    AssessInfoVO vo = assessInfo.buildVO();
                    if (currentUser != null) {
                        vo.setAvatar(currentUser.getAvatarurl());
                    } else {
                        vo.setAvatar("https://hospital-escort-001.oss-cn-hangzhou.aliyuncs.com/%E7%94%A8%E6%88%B7%E5%A4%B4%E5%83%8F.jpg");
                    }
                    return vo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 发布评论
     *
     * @param assessInfo
     */
    @Override
    @Transactional
    public void saveAssessInfo(AssessInfo assessInfo) {
        Asserts.notTrue(Objects.isNull(assessInfo.getEscortId()), "陪诊员id不能为空！");
        Asserts.hasText(assessInfo.getDescription(), "评价内容不允许为空");
        Asserts.notNull(assessInfo.getScore(), "评分不允许为空");

        AuthUser currentUser = WebSecurityUtil.getCurrentUser();
        assessInfo.setUserId(currentUser.getId());
        assessInfo.setUserName(StringUtils.isEmpty(currentUser.getUsername()) ?  currentUser.getNickname() : currentUser.getName());
        assessInfoMapper.insert(assessInfo);
    }

    /**
     * 删除评论
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteAssessInfo(Long id) {
        AuthUser user = WebSecurityUtil.getCurrentUser();
        AssessInfo assessInfo = assessInfoMapper.selectById(id);
        if (Objects.isNull(assessInfo)) {
            throw new BusinessException("评论不存在");
        }
        if (Objects.equals(user.getRole(), RoleEnum.ADMIN.getRole()) && Objects.equals(user.getId(), assessInfo.getUserId())) {
            assessInfoMapper.deleteById(id);
        } else if (Objects.equals(user.getRole(), RoleEnum.ADMIN.getRole())) {
            assessInfoMapper.deleteById(id);
        } else {
            throw new BusinessException("未知用户，无法删除！");
        }
    }
}
